import React, { Component } from 'react'
import { Redirect, Route, RouteProps } from 'react-router-dom'
import { getToken } from 'utils/storage'

export default class index extends Component<RouteProps> {
  render() {
    const { component: Component, ...rest } = this.props as any
    return (
      <Route
        {...rest}
        render={(props) => {
          const token = getToken()
          console.log(Component)
          return token ? (
            <Component {...props} />
          ) : (
            <Redirect
              to={{
                pathname: '/login',
                state: {
                  fromPath: props!.location!.pathname,
                },
              }}
            />
          )
        }}
      />
    )
  }
}
